﻿//import hu8.libs.js
//import hu8.log.js
//import hu8.box.js
//import /languages/zh.js
/**
* This file depended on jQuery, some attributes passed from the server end and a language file
* 
* @author: Jackie Liu
*
* The file manager can be called from another page, the page has the option to have a "page_type" js variable, 
* which is used to identify what kind of page the caller is. e.g. the file manager is initialised from the FCKeditor,
* then the code in this file would know how to deal with it. (how to display, what kind of functionalities to show, ect）
**/

var parent_type = "";


$(function () {
    parent_type = window.parent.page_type;
    if (parent_type) {
        log.log_text("从" + parent_type + "打开");
    }

    $(":wijmo-wijdialog").wijdialog("destroy").remove();
    filemanager.init(); //Initialise the file manager 初始化文件管理器

    /***************************** 按钮事件绑定 BUTTON EVENT BINDING *****************************/
    $(".fm-language-select-box").live("change", function () {
        var lang = $(this).val();
        window.location = "?lang=" + lang;
    });


    //Loading infomation 加载信息
    $(".cmd-loadinfo").live("click", function () {
        var dir = $(this).attr("rel");
        var is_dir = $(this).hasClass("dir");
        var title = $(this).text();
        filemanager.load_info(title, is_dir, dir);
    });

    $(".cmd-copy").live("click", function () {
        filemanager.relocate(false);
    });
    $(".cmd-cut").live("click", function () {
        filemanager.relocate(true);
    });
    $(".cmd-extract").live("click", function () {
        filemanager.extract(true);
    });
    $(".cmd-compress").live("click", function () {
        filemanager.compress(true);
    });
    $(".cmd-delete").live("click", function () {
        filemanager.remove(true);
    });
    $(".cmd-download").live("click", function () {
        filemanager.download(true);
    });
    $(".cmd-upload").live("click", function () {
        filemanager.upload(true);
    });
    $(".cmd-newfile").live("click", function () {
        filemanager.newfile(true);
    });
    $(".cmd-about").live("click", function () {
        var title = $(this).text();
        var path = $(this).attr("href");
        box.open_window(title, path, 400, 300);
        return false;
    });
    //open the config window
    $(".cmd-config").live("click", function () {
        var title = $(this).text();
        var path = $(this).attr("href");
        box.open_window(title, path, 400, 300);
        return false;
    });
    $(".cmd-newdirectory").live("click", function () {
        filemanager.newdirectory(true);
    });
    $(".cmd-change-mode").live("click", function () {
        var mode = $(this).attr("rel");
        filemanager.mode = mode;
        filemanager.reload();
        filemanager.init_mode();
    });
    /*******************************************************************/



    //导航
    $(".cmd-selectall").live("click", function () {
        $("#directory-content-list .row").addClass("ui-selected");
    });
    $(".cmd-reload").live("click", function () {
        filemanager.reload(true);
    });
    $(".cmd-home").live("click", function () {
        filemanager.load_contents("~/", true);
    });

    //上一级目录
    $(".cmd-uponelevel").live("click", function () {
        filemanager.uponelevel();
    });
    $(".cmd-uponelevel-dot").live("dblclick", function () {
        filemanager.uponelevel();
    });

    $(".cmd-back").live("click", function () {
        filemanager.back();
    });

    $(".cmd-forward").live("click", function () {
        filemanager.forward();
    });

    $(".cmd-jump").live("click", function () {
        var path = "~/" + $(".fe-current-path").val();
        filemanager.load_contents(path, true);
    });

    $(".fe-search").live("keyup", function () {
        var keyword = $(this).val().toLowerCase();
        $(".row").each(function () {
            if ($(this).attr("rel").toLowerCase().search(keyword) > -1) {
                $(this).show();
            } else {
                $(this).hide();
            }
        });
    });

    $(".fe-editfile").live("click", function () {
        var link = $(this).attr("rel");
        filemanager.editfile(link, true);
    });

    //右键按钮
    $(".row").live("mousedown", function (e) {
        if (e.which == 3) {
            filemanager.select_one($(this).attr("path"));
        }
    });
});

//加载文件或者文件夹信息
var filemanager = {};
filemanager.ajax_lock = false;
filemanager.mode = "thumb"; //table/thumb
filemanager.history = new Array;
filemanager.current_position = -1;
filemanager.base_url = fe_url + "/WebServices/FileService.asmx/";
filemanager.base_thumb_url = fe_url + "/Thumb/";
filemanager.get_url = function (url) {
    url = filemanager.base_url + url;
    return url;
}


//基础
filemanager.post = function (url, attrs, is_click, action) {
    url = filemanager.get_url(url);
    if (is_click) { //如果是手动点击的话，则有锁定功能
        if (!filemanager.ajax_lock) {
            filemanager.ajax_lock = true;
            filemanager.ajax_post(url, attrs, action);
        } else {
            log.log_text(lang.tips_wait_for_previous_request, true);
        }
    } else {
        filemanager.ajax_post(url, attrs, action);
    }
}

filemanager.ajax_post = function (url, attrs, action) {
    $.ajax({
        url: url,
        type: "POST",
        data: attrs,
        async: true,
        contentType: 'application/json; charset=utf-8',
        //dataType: 'json',
        success: action,
        complete: function () {
            filemanager.ajax_lock = false;
            log.log_text(lang.tips_request_completed, false);
        }
    });
}

filemanager.get = function (url, attrs, action) {
    url = filemanager.get_url(url);
    $.ajax({
        url: url,
        type: "GET",
        data: attrs,
        async: true, 
        contentType: 'application/json; charset=utf-8',
        //dataType: 'json',
        success: action
    });
}

/****************************************************************************************************************************/
filemanager.init = function () {
    filemanager.load_contents(filemanager.get_path(), true);
    filemanager.init_mode();
}

filemanager.init_mode = function () {
    $(".cmd-change-mode").show();
    $(".cmd-change-mode[rel='" + filemanager.mode + "']").hide();
}

filemanager.reload = function () {
    filemanager.load_contents(filemanager.get_path(), false);
}

//获取当前路径 Get the current path value
filemanager.get_path = function () {
    var current_path = $("#hfCurrentPath").val();
    return current_path;
}

//加载文件或文件夹信息 Load file/folder information
filemanager.load_info = function (title, is_dir, dir, is_click) {
    var attr = "{ path:'" + dir + "'}";
    if (is_dir) {
        filemanager.post("GetFolder", attrs, is_click, function (obj) {
            box.open(title, obj, true);
        });
    } else {
        filemanager.post("GetFile", attrs, is_click, function (obj) {
            box.open(title, obj, true);
        });
    }
}

//获取选中的文件以及文件夹爱 Get selected files and folders
filemanager.get_selected = function () {
    var files = "";
    $("#directory-content-list .ui-selected").each(function () {
        files += $(this).attr("path") + ",";
    });
    files = files.removeLastChar();
    return files;
}


filemanager.get_selected_list = function () {
    var files = "";
    $("#directory-content-list .ui-selected").each(function () {
        files += $(this).attr("path").replace("~","") + "<br/>";
    });
    return files;
}

filemanager.get_selected_count = function () {
    return $("#directory-content-list .ui-selected").size();
}




/****************************************************************************************************************************/

//加载文件夹列表
filemanager.load_contents = function (path, is_click) {
    var attr = "{ path:'" + path + "'}";
    filemanager.post("GetFolder", attr, is_click, function (d) {
        var folder = new Folder(d.d);

        if (!folder.hasError()) {
            var html = "";
            if (filemanager.mode == "table") {
                html = folder.generateTable();
            } else if (filemanager.mode == "thumb") {
                html = folder.generateThumbList();
            }

            $(".content-list").html(html);
            $(".fe-folder-info").html(folder.getMiniInfo());
            //历史记录
            var current_history = filemanager.history[filemanager.current_position];
            if (((current_history != null && path != filemanager.history[filemanager.current_position].getPath()) || current_history == null) && is_click) {
                filemanager.current_position++;
                filemanager.history[filemanager.current_position] = folder;
            }
            $(".fe-current-path").val(folder.getPath());
            $("#hfCurrentPath").val(folder.getRelativePath());

            //记录浏览记录到 log record browse history to log
            log.log_text(lang.browse_history + folder.getRelativePath());

            //加载多选器 initialise the multi-select
            filemanager.init_select();

            //加载文件夹右键菜单 initialise the right click menu
            var dir_menu = [];
            var dm_1 = {};
            dm_1[lang.open] = function (menuItem, menu) { filemanager.load_contents($(this).attr("path"), true); };
            dm_1[lang.download] = function (menuItem, menu) { filemanager.download($(this).attr("path")); };
            dir_menu.push(dm_1);
            //-------------------------------------------------
            dir_menu.push($.contextMenu.separator);
            var dm_2 = {};
            dm_2[lang.move] = function (menuItem, menu) { filemanager.relocate(true); };
            dm_2[lang.copy] = function (menuItem, menu) { filemanager.relocate(false); };
            dir_menu.push(dm_2);
            //-------------------------------------------------
            dir_menu.push($.contextMenu.separator);
            var dm_3 = {};
            dm_3[lang.del] = function (menuItem, menu) { filemanager.remove(); };
            dir_menu.push(dm_3);
            //-------------------------------------------------
            dir_menu.push($.contextMenu.separator);
            var dm_4 = {};
            dm_4[lang.compress] = function (menuItem, menu) { filemanager.compress(); };
            dm_4[lang.rename] = function (menuItem, menu) { filemanager.rename($(this).attr("path"), $(this).attr("rel")); };
            dir_menu.push(dm_4);
            //-------------------------------------------------
            dir_menu.push($.contextMenu.separator);
            var dm_5 = {};
            dm_5[lang.new_file] = function (menuItem, menu) { $(".cmd-newfile").click(); };
            dm_5[lang.new_folder] = function (menuItem, menu) { $(".cmd-newdirectory").click(); };
            dm_5[lang.view_property] = function (menuItem, menu) { filemanager.load_folder($(this).attr("path")); };
            dir_menu.push(dm_5);

            //加载文件右键菜单
            var file_menu = new Array;
            var fm_1 = {};
            if (parent_type == "THEME_ENGINE") {
                fm_1[lang.edit] = function (menuItem, menu) {
                    var full_path = $(this).attr("path").replace("~/", file_base_url);
                    parent.pe.insert_image(full_path);
                }; //从页面引擎呼叫
            }
            //-------------------------------------------------
            fm_1[lang.edit] = function (menuItem, menu) { filemanager.editfile($(this).attr("path")); };
            fm_1[lang.download] = function (menuItem, menu) { filemanager.download($(this).attr("path")); };
            file_menu.push(fm_1);
            file_menu.push($.contextMenu.separator);
            //-------------------------------------------------
            var fm_2 = {};
            fm_2[lang.move] = function (menuItem, menu) { filemanager.relocate(true); };
            fm_2[lang.copy] = function (menuItem, menu) { filemanager.relocate(false); };
            file_menu.push(fm_2);
            file_menu.push($.contextMenu.separator);
            //-------------------------------------------------
            var fm_3 = {};
            fm_3[lang.del] = function (menuItem, menu) { filemanager.remove(); };
            fm_3[lang.rename] = function (menuItem, menu) { filemanager.rename($(this).attr("path"), $(this).attr("rel")); };
            file_menu.push(fm_3);
            file_menu.push($.contextMenu.separator);
            //-------------------------------------------------
            var fm_4 = {};
            fm_4[lang.new_file] = function (menuItem, menu) { $(".cmd-newfile").click(); };
            fm_4[lang.new_folder] = function (menuItem, menu) { $(".cmd-newdirectory").click();  };
            fm_4[lang.view_property] = function (menuItem, menu) { filemanager.load_file($(this).attr("path")); };
            file_menu.push(fm_4);

            $(".fe-dir").contextMenu(dir_menu, { theme: 'vista' });
            $(".fe-file").contextMenu(file_menu, { theme: 'vista' });
        } else {
            box.error(folder.getError());
        }
    });
}

//只选中一个文件或文件夹 select only one file or folder
filemanager.select_one = function (path) {
    $(".row").removeClass("ui-selected");
    $(".row[path='"+ path +"']").addClass("ui-selected");
}

//加载文件属性 Load file properties
filemanager.load_file = function (path, is_click) {
    var attr = "{ path:'" + path + "'}";
    filemanager.post("GetFileInfo", attr, is_click, function (d) {
        var file = new File(d.d);
        var title = file.getName() + " " + lang.property;
        var content = file.getInfo();
        //弹框按钮
        var buttons = {};
        buttons[lang.confirm] = function () {
            filemanager.set_file_attributes(this);
            $(this).wijdialog("close");
        };
        buttons[lang.cancel] = function () {
            $(this).wijdialog("close");
        };
        buttons[lang.apply] = function () {
            filemanager.set_file_attributes(this);
        };

        box.dialog(title, content, {
            autoOpen: true,
            modal: true,
            width: 550,
            height: 500,
            buttons: buttons,
            captionButtons: {
                pin: { visible: false },
                refresh: { visible: false },
                toggle: { visible: false },
                minimize: { visible: false },
                maximize: { visible: false }
            }
        });
    });
}
//加载文件夹属性 Load Folder properties
filemanager.load_folder = function (path, is_click) {
    var attr = "{ path:'" + path + "'}";
    filemanager.post("GetFolderInfo", attr, is_click, function (d) {
        var folder = new Folder(d.d);
        var title = folder.getName() + " " + lang.property;
        var content = folder.getInfo();

        //弹框按钮
        var buttons = {};
        buttons[lang.confirm] = function () {
            filemanager.set_folder_attributes_action(this);
            $(this).wijdialog("close");
        };
        buttons[lang.cancel] = function () {
            $(this).wijdialog("close");
        };
        buttons[lang.apply] = function () {
            filemanager.set_folder_attributes_action(this);
        };

        //创建窗口
        box.dialog(title, content, {
            autoOpen: true,
            modal: true,
            width: 550,
            height: 500,
            buttons: buttons,
            captionButtons: {
                pin: { visible: false },
                refresh: { visible: false },
                toggle: { visible: false },
                minimize: { visible: false },
                maximize: { visible: false }
            }
        });
    });
}


//设置文件夹的属性 Updae directory properties
filemanager.set_folder_attributes_action = function (win) {
    //弹框按钮
    var buttons = {};
    buttons[lang.yes] = function () {
        filemanager.set_folder_attributes(win, true);
        $(this).wijdialog("close");
    };
    buttons[lang.no] = function () {
        filemanager.set_folder_attributes(win, false);
        $(this).wijdialog("close");
    };
    buttons[lang.cancel] = function () {
        $(this).wijdialog("close");
    };

    box.dialog(lang.question, lang.question_apply_to_subfolders, {
        autoOpen: true,
        modal: true,
        width: 250,
        height: 150,
        buttons: buttons,
        captionButtons: {
            pin: { visible: false },
            refresh: { visible: false },
            toggle: { visible: false },
            minimize: { visible: false },
            maximize: { visible: false }
        }
    });
}

//设定文件夹读写权限
filemanager.set_folder_attributes = function (win, recursive, is_click) {
    var is_readonly = $(win).find(".fe-isreadonly").is(":checked");
    var is_hidden = $(win).find(".fe-ishidden").is(":checked");
    var is_system = $(win).find(".fe-issystem").is(":checked");
    var is_archive = $(win).find(".fe-isarchive").is(":checked");
    var is_writable = $(win).find(".fe-app-iswritable").is(":checked");
    var is_browsable = $(win).find(".fe-app-isbrowsable").is(":checked");
    var path = $(win).find(".fe-propertypath").val();
    var data = "{ path:'" + path + "',isHidden:" + is_hidden + ", isReadOnly:" + is_readonly + ", isSystem:" + is_system + ", isArchive:" + is_archive + ", recursive:" + recursive + ", isWritable:" + is_writable + ", isBrowsable:" + is_browsable + "}";
    filemanager.post("SetFolderAttributes", data, is_click, function (d) {
        if (d.d.success) {
            box.alert(lang.success, d.d.message);
        } else {
            box.error(d.d.message);
        }
        filemanager.reload();
    });
}


//设定文件读写权限
filemanager.set_file_attributes = function (win, is_click) {
    var is_readonly = $(win).find(".fe-isreadonly").is(":checked");
    var is_hidden = $(win).find(".fe-ishidden").is(":checked");
    var is_system = $(win).find(".fe-issystem").is(":checked");
    var is_archive = $(win).find(".fe-isarchive").is(":checked");
    var is_writable = $(win).find(".fe-app-iswritable").is(":checked");
    var is_browsable = $(win).find(".fe-app-isbrowsable").is(":checked");
    var path = $(win).find(".fe-propertypath").val();
    var data = "{ path:'" + path + "',isHidden:" + is_hidden + ", isReadOnly:" + is_readonly + ", isSystem:" + is_system + ", isArchive:" + is_archive + ", isWritable:" + is_writable + ", isBrowsable:" + is_browsable + "}";
    filemanager.post("SetFileAttributes", data, is_click, function (d) {
        if (d.d.success) {
            box.alert(lang.success, d.d.message);
        } else {
            box.error(d.d.message);
        }
        filemanager.reload();
    });
}


//上一级目录
filemanager.uponelevel = function () {
    var current_path = filemanager.get_path();
    if (current_path.endsWith("/")) {
        current_path = current_path.removeLastChar();
    }
    var parts = current_path.split('/');
    var path = "";
    if (parts.length > 1) {
        for (var i = 0; i <= (parts.length - 2); i++) {
            path += parts[i];
            path += "/";
        }
    } else {
        //show you are at root folder tip
        box.alert(lang.tips, lang.tips_you_are_at_root);
        path = "~/";
    }
    filemanager.load_contents(path, true);
}

//上一页
filemanager.back = function () {
    if (filemanager.current_position > 0 && filemanager.current_position < filemanager.history.length) {
        filemanager.current_position -= 1;
        var path = filemanager.history[filemanager.current_position].getRelativePath();
        filemanager.load_contents(path, false);
    }
}
//上一页
filemanager.forward = function () {
    if (filemanager.current_position >= 0 && filemanager.current_position < (filemanager.history.length - 1)) {
        filemanager.current_position += 1;
        var path = filemanager.history[filemanager.current_position].getRelativePath();
        filemanager.load_contents(path, false);
    }
}


/****************************************************************************************************************************/

//压缩
filemanager.compress = function (is_click) {
    var count = filemanager.get_selected_count();
    if (count > 0) {
        var html = "<div class='fe-notes'>";
        html += "<strong>" + lang.tips_zip_confirm_message + "<input class='fe-zipname' typed='text'/>.zip</strong><br/>";
        html += "<div class='fe-filelist'>";
        html += filemanager.get_selected_list();
        html += "</div>";
        html += "</div>";

        var buttons = {}
        buttons[lang.confirm] = function () {
            var selected = filemanager.get_selected();
            var zip_name = $(this).find(".fe-zipname").val();
            var this_box = this;
            if (zip_name) {
                zip_name += ".zip";
                var path = filemanager.get_path(); //直接压缩到本文件夹
                filemanager.post("Compress", "{ paths:'" + selected + "', dest:'" + path + "', zipName:'" + zip_name + "'}", is_click, function (d) {
                    if (d.d.success) {
                        box.alert(lang.tips_zip_success, d.d.message);
                        filemanager.reload();
                        $(this_box).wijdialog("close");
                    } else {
                        box.error(d.d.message);
                    }
                });
            } else {
                box.error(lang.tips_enter_file_name);
            }
        };
        buttons[lang.cancel] = function () {
            $(this).wijdialog("close");
        };

        box.dialog(lang.tips_zip_confirm, html, {
            autoOpen: true,
            modal: true,
            width: 500,
            buttons: buttons,
            captionButtons: {
                pin: { visible: false },
                refresh: { visible: false },
                toggle: { visible: false },
                minimize: { visible: false },
                maximize: { visible: false }
            }
        });
    } else {
        //Please first select one to many files or folders
        box.error(lang.tips_select_file_folder_name);
    }
}

//解压缩 Extract zip file
filemanager.extract = function (is_click) {
    var count = filemanager.get_selected_count();
    var selected_zip = filemanager.get_selected();
    var html = "<ul class='fe-form'>";
    html += "<li><label>" + lang.zip_file + "</label>" + selected_zip + "</li>";
    html += "<li><label>" + lang.extact_location + "</label>/ <input class='fe-extractto' type='text' value='" + filemanager.get_path().replace("~/", "") + "'/></li>";
    html += "<li><label>" + lang.replace_current_files + "</label><input class='fe-extractoverwrite' type='checkbox' checked='checked'/></li>";
    html += "</ul>";
    if (count == 1 && selected_zip.endsWith(".zip")) {
        var buttons = {};
        buttons[lang.confirm] = function () {
            var extract_to = "~/" + $(this).find(".fe-extractto").val();
            var this_box = this;
            var overwrite = $(this).find(".fe-extractoverwrite").is(":checked");
            var path = filemanager.get_path(); //直接压缩到本文件夹
            filemanager.post("Extract", "{ path:'" + selected_zip + "', dest:'" + extract_to + "', overwrite:" + overwrite + "}", is_click, function (d) {
                if (d.d.success) {
                    box.alert(lang.extract_successful, d.d.message);
                    filemanager.reload();
                    $(this_box).wijdialog("close");
                } else {
                    box.error(d.d.message);
                }
            });
        };
        buttons[lang.cancel] = function () {
            $(this).wijdialog("close");
        };


        box.dialog(lang.extract_file, html, {
            autoOpen: true,
            modal: true,
            width: 500,
            buttons: buttons,
            captionButtons: {
                pin: { visible: false },
                refresh: { visible: false },
                toggle: { visible: false },
                minimize: { visible: false },
                maximize: { visible: false }
            }
        });

    } else {
        //please select a zip file for extraction
        box.error(lang.tips_select_zip);
    }
}

/****************************************************************************************************************************/

//移动，拷贝
filemanager.relocate = function (is_move, is_click) {
    var count = filemanager.get_selected_count();
    var heading = is_move ? lang.move : lang.copy;
    if (count > 0) {
        var count = filemanager.get_selected_count();
        if (count > 0) {
            var buttons = {};
            buttons[lang.confirm] = function () {
                var selected = filemanager.get_selected();
                var newpath = $(this).find(".fe-newpath").val();
                var url = is_move ? "Cut" : "Copy";
                var this_box = this;
                filemanager.post(url, "{ paths:'" + selected + "', newPath:'~/" + newpath + "'}", is_click, function (d) {
                    if (d.d.success) {
                        box.alert(heading + lang.success, d.d.message);
                    } else {
                        box.error(d.d.message);
                    }
                    $(this_box).wijdialog("close");
                    filemanager.reload();
                });
            };

            buttons[lang.cancel] = function () {
                $(this).wijdialog("close");
            };


            var html = "<div class='fe-notes'>";
            html += "<strong>" + lang.are_you_sure + heading + lang.the_following_file_to + " /<input type='textbox' class='fe-newpath'/>：</strong><br/>";
            html += "<div class='fe-filelist'>";
            html += filemanager.get_selected_list();
            html += "</div>";
            html += "</div>";
            box.dialog(lang.confirm + " " + heading, html, {
                autoOpen: true,
                modal: true,
                width: 500,
                buttons: buttons,
                captionButtons: {
                    pin: { visible: false },
                    refresh: { visible: false },
                    toggle: { visible: false },
                    minimize: { visible: false },
                    maximize: { visible: false }
                }
            });
        } else {
            box.error(lang.tips_select_file_folder_name);
        }
    } else {
        box.error(lang.tips_select_file_folder_name);
    }
}

//删除
filemanager.remove = function (is_click) {
    var count = filemanager.get_selected_count();
    if (count > 0) {
        var html = "<div class='fe-notes'>";
        //html += "<strong>"+ lang.tips_confirm_delete +"</strong><br/>";
        html += "<strong>" + lang.tips_delete_subfolders + "<input type='checkbox' class='fe-delete-recursive'/></strong><br/>";
        html += "<div class='fe-filelist'>";
        html += filemanager.get_selected_list();
        html += "</div>";
        html += "</div>";

        var buttons = {};

        buttons[lang.confirm] = function () {
            var paths = filemanager.get_selected();
            var recursive = $(this).find(".fe-delete-recursive").is(":checked");
            var this_box = this;
            filemanager.post("Delete", "{ paths:'" + paths + " ', recursive: " + recursive + "}", is_click, function (d) {
                if (d.d.success) {
                    box.alert(lang.success, d.d.message);
                    filemanager.reload();
                }
                else {
                    box.error(d.d.message);
                }
                $(this_box).wijdialog("close");
            });
        };
        buttons[lang.cancel] = function () {
            $(this).wijdialog("close");
        };

        box.dialog(lang.tips_confirm_delete, html, {
            autoOpen: true,
            modal: true,
            width: 500,
            buttons: buttons,
            captionButtons: {
                pin: { visible: false },
                refresh: { visible: false },
                toggle: { visible: false },
                minimize: { visible: false },
                maximize: { visible: false }
            }
        });
    } else {
        box.error(lang.tips_select_file_folder_name);
    }
}

//重命名
filemanager.rename = function (relativePath, name, is_click) {
    var html = "<ul class='fe-form'>";
    html += "<li><label>" + lang.new_name + "</label><input class='fe-newname' type='text' value='" + name + "' /></li>";
    html += "<li class='split'></li>";
    html += "<li><label>" + lang.location + "</label>" + relativePath + "</li>";
    html += "<input class='fe-newnamepath' type='hidden' value='" + relativePath + "'/>";
    html += "<li><label>" + lang.original_name + "</label>" + name + "</li>";
    html += "</ul>";

    var buttons = {};
    buttons[lang.confirm] = function () {
        var newname = $(this).find(".fe-newname").val();
        var path = $(this).find(".fe-newnamepath").val();
        var this_box = this;
        if (newname) {
            var data = "{ path:'" + path + "', newName:'" + newname + "'}";
            filemanager.post("Rename", data, is_click, function (d) {
                if (d.d.success) {
                    box.alert(lang.success, d.d.message);
                    filemanager.reload();
                    $(this_box).wijdialog("close");
                } else {
                    box.error(d.d.message);
                }
            });
        } else {
            box.error(lang.file_name_cannot_be_null);
        }
    };
    buttons[lang.cancel] = function () {
        $(this).wijdialog("close");
    };


    box.dialog(lang.rename + name, html, {
        autoOpen: true,
        modal: true,
        width: 500,
        height: 220,
        buttons: buttons,
        captionButtons: {
            pin: { visible: false },
            refresh: { visible: false },
            toggle: { visible: false },
            minimize: { visible: false },
            maximize: { visible: false }
        }
    });
}
//新文件
filemanager.newfile = function (is_click) {
    var html = "<ul class='fe-form'>";
    html += "<li><label>" + lang.file_name + "</label><input type='text' class='fe-newfile-name'/></li>";
    html += "</ul>";
    var buttons = {};
    buttons[lang.confirm] = function () {
        var name = $(this).find(".fe-newfile-name").val();
        var this_box = this;
        filemanager.post("NewFile", "{ path:'" + filemanager.get_path() + "', name:'" + name + "'}", is_click, function (d) {
            if (d.d.success) {
                $(this_box).wijdialog("close");
                filemanager.reload(); //成功之后刷新
                box.alert(lang.success, d.d.message);
            } else {
                box.error(d.d.message);
            }
        });
    };
    buttons[lang.cancel] = function(){
        $(this).wijdialog("close");
    };


    box.dialog(lang.new_name, html, {
        autoOpen: true,
        modal: true,
        width: 500,
        height: 180,
        buttons: buttons,
        captionButtons: {
            pin: { visible: false },
            refresh: { visible: false },
            toggle: { visible: false },
            minimize: { visible: false },
            maximize: { visible: false }
        }
    });

}
//新文件夹
filemanager.newdirectory = function (is_click) {
    var html = "<ul class='fe-form'>";
    html += "<li><label>" + lang.title + "</label><input type='text' class='fe-newdirectory-name'/></li>";
    html += "</ul>";

    var buttons = {};
    buttons[lang.confirm] = function () {
        var name = $(this).find(".fe-newdirectory-name").val();
        var this_box = this;
        filemanager.post("NewFolder", "{ path:'" + filemanager.get_path() + "', name:'" + name + "'}", is_click, function (d) {
            if (d.d.success) {
                filemanager.reload(); //成功之后刷新
                box.alert(lang.success, d.d.message);
                $(this_box).wijdialog("close");
            } else {
                box.error(d.d.message);
            }
        });
    };
    buttons[lang.cancel] = function () {
        $(this).wijdialog("close");
    };

    box.dialog(lang.new_folder, html, {
        autoOpen: true,
        modal: true,
        width: 500,
        height: 180,
        buttons: buttons,
        captionButtons: {
            pin: { visible: false },
            refresh: { visible: false },
            toggle: { visible: false },
            minimize: { visible: false },
            maximize: { visible: false }
        }
    });

}
//编辑文件
filemanager.editfile = function (url, is_click) {
    if (url.endsWith("txt") || url.endsWith("xml") || url.endsWith("config") || url.endsWith("html")
     || url.endsWith("htm") || url.endsWith("js") || url.endsWith("css") || url.endsWith("php")) {
        box.open_window(lang.edit_text_file, fe_url + "/TextEditor.aspx?Path=" + url, 690, 545, true);
    } else {
        box.error(lang.tips_file_format_not_supported);
    }
}

//上传文件
filemanager.upload = function () {
    var path = encodeURI(filemanager.get_path());
    box.open_window(lang.upload_files, fe_url + "/FileUploader.aspx?path=" + path, 500, 400, true);
}

//下载文件
filemanager.download = function (is_click) {
    if (filemanager.get_selected_count() > 0) {
        var paths = filemanager.get_selected();
        filemanager.post("DownloadFiles", "{paths:'" + paths + "'}", is_click, function (d) {
            if (d.d.success) {
                var html = "<a href='" + d.d.message + "' target='_blank'>" + lang.tips_click_here_to_download + d.d.message + "</a>";
                box.alert(lang.download_file, html);
            } else {
                box.error(d.d.message);
            }
        });
    } else {
        box.error(lang.tips_select_files);
    }
}


//初始化多选
filemanager.init_select = function () {
    $("#directory-content-list").selectable({ filter: '.row' });
}

// class file
var File = function (fileObj) {
    var fileObj = fileObj;

    this.isImage = function () {
        var exts = ".jpg,.jpeg,.png,.gif,.bmp,.svg";
        return exts.indexOf(fileObj.Extension) > -1;
    }
    this.isText = function () {
        var exts = ".txt,.html,.js,.css,.htmlx,.htm,.xml,.config,.php,.vb,.cs,.sql,.asp,.aspx";
        return exts.indexOf(fileObj.Extension) > -1;
    }
    this.getName = function () {
        return fileObj.Name;
    }
    this.getPath = function () {
        return fileObj.RelativePath.replace("~/", "");
    }
    this.getRelativePath = function () {
        return fileObj.RelativePath;
    }
    this.getThumbnailPath = function () {
        return filemanager.base_thumb_url + fileObj.ThumbnailName;
    }

    this.getInfo = function () {
        var html = "";
        if (this.isImage()) {
            html += "<img src='" + this.getThumbnailPath() + "' class='fe-thumbnail'/>";
        }
        html += "<ul class='fe-form'>";
        html += "<li><label>"+ lang.file_name +"</label>" + fileObj.Name + "</li>";
        html += "<li><label>"+ lang.file_ext +"</label>" + fileObj.Extension + "</li>";
        html += "<li class='split'></li>";
        html += "<li><label>"+ lang.file_path +"</label>" + fileObj.Path + "</li>";
        html += "<input class='fe-propertypath' type='hidden' value='" + fileObj.RelativePath + "'/>";
        html += "<li><label>"+ lang.file_size +"</label>" + fileObj.SizeString + "(" + fileObj.Size + lang.bytes +")</li>";
        html += "<li class='split'></li>";
        html += "<li><label>"+ lang.file_created +"</label>" + fileObj.CreatedString + "</li>";
        html += "<li><label>"+ lang.file_last_updated +"</label>" + fileObj.LastModifiedString + "</li>";
        html += "<li><label>"+ lang.file_last_visited +"</label>" + fileObj.LastAccessedString + "</li>";
        html += "<li class='split'></li>";
        if (this.isText()) {
            html += "<li><label>" + lang.operation + "</label><a class='fe-editfile' rel='" + this.getRelativePath() + "' href='javascript:void(0)'>" + lang.edit + "</a></li>";
            html += "<li class='split'></li>";
        }
        html += "<li><label>"+ lang.read_only +"</label><input class='fe-isreadonly' type='checkbox' " + (fileObj.IsReadOnly ? "checked='checked'" : "") + "/>"+ lang.read_only_explain +" </li>";
        html += "<li><label>" + lang.hidden + "</label><input class='fe-ishidden' type='checkbox' " + (fileObj.IsHidden ? "checked='checked'" : "") + "/>" + lang.hidden_explain + "</li>";
        html += "<li><label>"+ lang.system +"</label><input class='fe-issystem' type='checkbox' " + (fileObj.IsSystem ? "checked='checked'" : "") + "/></li>";
        html += "<li><label>"+ lang.archive +"</label><input class='fe-isarchive' type='checkbox' " + (fileObj.IsArchive ? "checked='checked'" : "") + "/></li>";
        html += "<li class='split'></li>";
        html += "<li><label>" + lang.readable + "</label><input class='fe-app-iswritable' type='checkbox' " + (fileObj.IsWritable ? "checked='checked'" : "") + "/> " + lang.writable_explain + "</li>";
        html += "<li><label>" + lang.writable + "</label><input class='fe-app-isbrowsable' type='checkbox' " + (fileObj.IsBrowsable ? "checked='checked'" : "") + "/> " + lang.readable_explain + "</li>";
        html += "</ul>";
        html += fileObj.Permission;
        return html;
    }
}


//class Folder
var Folder = function (folderObj) {
    var folderObj = folderObj;
    this.hasError = function(){
        return folderObj.ErrorOccured;
    }
    this.getError = function(){
        return folderObj.Error;
    }

    this.getName = function () {
        return folderObj.Name;
    }

    this.getPath = function () {
        return folderObj.RelativePath.replace("~/", "");
    }

    this.getRelativePath = function () {
        return folderObj.RelativePath;
    }

    //生成列表
    this.generateTable = function () {
        var html = "<table class='directory-content'>";
        html += "<thead>";
        html += "<tr><th>" + lang.file_name + "</th><th width='150'>" + lang.file_size + "</th><th width='120'>" + lang.file_last_updated + "</th><th width='80'>" + lang.file_ext + "</th><th width='50'>" + lang.file_permission + "</th></tr>";
        html += "</thead>";
        //上级目录
        html += "<tr class='cmd-uponelevel-dot'>";
        html += "<td><a class='dir' href='javascript:void(0)'>....</a></td>";
        html += "<td></td>";
        html += "<td></td>";
        html += "<td></td>";
        html += "<td></td>";
        html += "</tr>";
        html += "<tbody id='directory-content-list'>";
        $.each(folderObj.Folders, function () {
            html += "<tr class='fe-dir row' rel='" + this.Name + "' path='" + this.RelativePath + "'>";
            html += "<td><a class='dir' rel='" + this.RelativePath + "' href='javascript:void(0)'>" + this.Name + "</a></td>";
            html += "<td>" + lang.sub_file + ": " + this.FileCount + " " + lang.sub_folder + ": " + this.FolderCount + "</td>";
            html += "<td>" + this.LastModifiedString + "</td>";
            html += "<td></td>";
            html += "<td>" + (this.IsHidden ? "<div class='invisible'></div>" : "<div class='visible'></div>") + (this.IsReadOnly ? "" : "<div class='editable'></div>") + "</td>";
            html += "</tr>";
        });
        $.each(folderObj.Files, function () {
            html += "<tr class='fe-file row' rel='" + this.Name + "' path='" + this.RelativePath + "'>";
            html += "<td><a class='ico-" + this.Type + "' rel='" + this.RelativePath + "' href='javascript:void(0)'>" + this.Name + "</a></td>";
            html += "<td>" + this.SizeString + "</td>";
            html += "<td>" + this.LastModifiedString + "</td>";
            html += "<td>" + this.Extension + "</td>";
            html += "<td>" + (this.IsHidden ? "<div class='invisible'></div>" : "<div class='visible'></div>") + (this.IsReadOnly ? "" : "<div class='editable'></div>") + "</td>";
            html += "</tr>";
        });
        html += "</tbody>";
        html += "</table>";
        return html;
    }

    this.generateThumbList = function () {
        var html = "<div class='directory-content'>";
        //上级目录
        html += "<div class='cmd-uponelevel-dot'></div>";
        html += "<div id='directory-content-list'>";
        $.each(folderObj.Folders, function () {
            html += "<div class='fe-dir row' rel='" + this.Name + "' path='" + this.RelativePath + "'>";
            html += "<div class='fe-thumb'><img src='/FileExplorer/Thumb/icons/directory.png' /></div>";
            html += "<div class='fe-row-name'><a rel='" + this.RelativePath + "' href='javascript:void(0)'>" + this.Name + "</a></div>";
            html += "<div class='fe-row-size'></div>";
            html += "<div class='fe-row-permission'>" + (this.IsHidden ? "<div class='invisible'></div>" : "<div class='visible'></div>") + (this.IsReadOnly ? "" : "<div class='editable'></div>") + "</div>";
            html += "</div>";
        });
        $.each(folderObj.Files, function () {
            html += "<div class='fe-file row' rel='" + this.Name + "' path='" + this.RelativePath + "'>";
            html += "<div class='fe-thumb'><img src='" + filemanager.base_thumb_url + this.ThumbnailName + "' /></div>";
            html += "<div class='fe-row-name'><a rel='" + this.RelativePath + "' href='javascript:void(0)'>" + this.Name + "</a></div>";
            html += "<div class='fe-row-size'>" + this.SizeString + "</div>";
            html += "<div class='fe-row-permission'>" + (this.IsHidden ? "<div class='invisible'></div>" : "<div class='visible'></div>") + (this.IsReadOnly ? "" : "<div class='editable'></div>") + "</div>";
            html += "</div>";
        });
        html += "</div>";
        html += "</div>";
        return html;
    }

    this.getMiniInfo = function () {
        var html = "<strong>" + lang.sub_folder + "：</strong>" + folderObj.FolderCount + " | <strong>" + lang.sub_file + "：</strong>" + folderObj.FileCount;
        return html;
    }

    this.getInfo = function () {
        var html = "";
        html += "<ul class='fe-form'>";
        html += "<li><label>"+ lang.folder +"</label>" + folderObj.Name + "</li>";
        html += "<li class='split'></li>";
        html += "<li><label>"+ lang.file_path +"</label>" + folderObj.Path + "</li>";
        html += "<input class='fe-propertypath' type='hidden' value='" + folderObj.RelativePath + "'/>";
        //html += "<li><label>" + lang.file_size + "</label>" + folderObj.SizeString + "(" + folderObj.Size + " "+ lang.bytes +")</li>";
        html += "<li><label>"+ lang.sub_file +"</label>" + folderObj.FileCount + "</li>";
        html += "<li><label>"+ lang.sub_folder +"</label>" + folderObj.FolderCount + "</li>";
        html += "<li class='split'></li>";
        html += "<li><label>"+ lang.file_created +"</label>" + folderObj.CreatedString + "</li>";
        html += "<li><label>"+ lang.file_last_updated +"</label>" + folderObj.LastModifiedString + "</li>";
        html += "<li><label>"+ lang.file_last_visited +"</label>" + folderObj.LastAccessedString + "</li>";
        html += "<li class='split'></li>";
        html += "<li><label>"+ lang.read_only +"</label><input class='fe-isreadonly' type='checkbox' " + (folderObj.IsReadOnly ? "checked='checked'" : "") + "/> "+ lang.read_only_explain +"</li>";
        html += "<li><label>"+ lang.hidden +"</label><input class='fe-ishidden' type='checkbox' " + (folderObj.IsHidden ? "checked='checked'" : "") + "/> "+ lang.hidden_explain +"</li>";
        html += "<li><label>"+ lang.system +"</label><input class='fe-issystem' type='checkbox' " + (folderObj.IsSystem ? "checked='checked'" : "") + "/></li>";
        html += "<li><label>"+ lang.archive +"</label><input class='fe-isarchive' type='checkbox' " + (folderObj.IsArchive ? "checked='checked'" : "") + "/></li>";
        html += "<li class='split'></li>";
        html += "<li><label>"+ lang.writable +"</label><input class='fe-app-iswritable' type='checkbox' " + (folderObj.IsWritable ? "checked='checked'" : "") + "/> "+ lang.writable_explain +"</li>";
        html += "<li><label>"+ lang.readable +"</label><input class='fe-app-isbrowsable' type='checkbox' " + (folderObj.IsBrowsable ? "checked='checked'" : "") + "/> "+ lang.readable_explain +"</li>";
        html += "</ul>";
        html += folderObj.Permission;
        return html;
    }
}


//文件列表的导航条高度 2011-10-12 xiaobiqin
//Some UI fix
$(function () {
    var Resize = function () {
        //alert($(window).height());
        var i = (($(window).height()) - 230);
        //alert(i);
        if (i >= 5000) {
            $(".directory-content").height("500px");
            //alert($(".directory-content").height(i));
        } else {
            $(".directory-content").height(i);
        }

    }
    if (document.all) {
        window.attachEvent("onscroll", function () { Resize(); });
        window.attachEvent("onresize", function () { Resize(); });
    } else {
        window.addEventListener("scroll", function () { Resize(); }, true);
        window.addEventListener("resize", function () { Resize(); }, true);
    }
    Resize();
});
